﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1.Admin
{
    public partial class ManageRoles1 : System.Web.UI.Page
    {
        MemberAL memberAL = new MemberAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            //memberAL = new MemberAL();
            List<RoleDTO> allRoles = memberAL.getAllRoles();
            if (allRoles.Count > 0)
            {
                gvRoles.DataSource = allRoles;
                gvRoles.DataBind();
            }
        }

        protected void gvRoles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void gvRoles_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvRoles.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = gvRoles.Rows[e.RowIndex];
            var x = (HiddenField)row.FindControl("hdRoleId");
            Guid roleId = new Guid(x.Value.ToString());
            String result = memberAL.deleteRole(roleId);
            
            if(result =="success")
            {
                lblRoleResult.Text="The role has been successfully deleted";
            }
            else if(result =="error")
            {
                lblRoleResult.Text="There is an error occured while deleting the record";
            }
            else if(result=="norecord")
            {
                lblRoleResult.Text = "There is no record found";
            }
            gvRoles.EditIndex = -1;
            BindData();
        }

        protected void gvRoles_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRoles.EditIndex = -1;
            BindData();
        }

        protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvRoles.Rows[e.RowIndex];
            HiddenField hiddenId = (HiddenField)row.FindControl("hdRoleId");
            Guid roleId = new Guid(hiddenId.Value.ToString());
            TextBox txtRoleName = (TextBox)row.FindControl("txtRoleName");
            TextBox txtRoleDesc = (TextBox)row.FindControl("txtRoleDesc");
            RoleDTO roleDTO = new RoleDTO
            {
                roleId = roleId,
                roleName = txtRoleName.Text,
                Description = txtRoleDesc.Text,
                ModifiedAt = DateTime.Now
            };
            String result = memberAL.SaveRole(roleDTO);
            if (result == "Updated")
            {
                lblRoleResult.Text = "Role details has been successfully updated!!!";
            }
            gvRoles.EditIndex = -1;
            BindData();
        }

        protected void AddNewRecord(object sender, EventArgs e)
        {

            gvRoles.ShowFooter = true;

            BindData();

        }

        protected void gvRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EmptyInsert"))
            {

                TextBox role = gvRoles.Controls[0].Controls[0].FindControl("txtRoleName") as TextBox;

                TextBox desc = gvRoles.Controls[0].Controls[0].FindControl("txtRoleDescription") as TextBox;



                Control rowContorl = gvRoles.Controls[0].Controls[0];
                var s = (HiddenField)rowContorl.FindControl("hdRoleId");
                Guid roleId = new Guid(s.Value.ToString());
                TextBox txtRoleName = (TextBox)rowContorl.FindControl("txtRoleName");
                TextBox txtRoleDesc = (TextBox)rowContorl.FindControl("txtRoleDesc");
                RoleDTO roleDTO = new RoleDTO
                {
                    roleId = roleId,
                    roleName = txtRoleName.Text,
                    Description = txtRoleDesc.Text,
                    ModifiedAt = DateTime.Now
                };
                String result = memberAL.SaveRole(roleDTO);
                if (result == "Updated")
                {
                    lblRoleResult.Text = "Role details has been successfully updated!!!";
                }
                gvRoles.EditIndex = -1;

                lblRoleResult.Text = "<br />Record inserted successfully<br />";

                BindData();// rebind the dat

            }

            if (e.CommandName.Equals("Insert"))
            {

            //    TextBox role = gvRoles.FooterRow.FindControl("txtRoleName") as TextBox;

            //    TextBox desc = gvRoles.FooterRow.FindControl("txtRoleDescription") as TextBox;



            //    Control rowControl = gvRoles.Controls[0].Controls[0];
            //    var s = (HiddenField)rowControl.FindControl("hdRoleId");
            //    Guid roleId = new Guid(s.Value.ToString());
            //    TextBox txtRoleName = (TextBox)rowControl.FindControl("txtRoleName");
            //    TextBox txtRoleDesc = (TextBox)rowControl.FindControl("txtRoleDesc");

                //HiddenField hfRoleId = gvRoles.FooterRow.FindControl("hdRoleId") as HiddenField;
                //Guid hroleId = new Guid(hfRoleId.Value.ToString());
                TextBox txRoleName = gvRoles.FooterRow.FindControl("txtRoleName") as TextBox;
                TextBox txRoleDesc = gvRoles.FooterRow.FindControl("txtRoleDesc") as TextBox;
                RoleDTO roleDTO = new RoleDTO
                {
                    roleId = Guid.NewGuid(),
                    roleName = txRoleName.Text,
                    Description = txRoleDesc.Text,
                    ModifiedAt = DateTime.Now
                };
                String result = memberAL.SaveRole(roleDTO);
                if (result == "Updated")
                {
                    lblRoleResult.Text = "Role details has been successfully updated!!!";
                }
                gvRoles.EditIndex = -1;

                lblRoleResult.Text = "<br />Record inserted successfully<br />";

                BindData(); // rebind the data

            }
        }

     
    }
}